//
//  JWHongSearchResultVC.m
//  koubei
//
//  Created by 李彦宏 on 2018/5/16.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "JWHongSearchResultVC.h"
#import "LMDataSource.h"
#import "SDUserModel.h"
#import "JWSearcell.h"
#import "JWStudentInfoCtry.h"
#import "JWUserManager.h"


static NSString *const reuserId = @"reuserId";
@interface JWHongSearchResultVC ()<UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) LMDataSource *dataSource;
@property (nonatomic, strong) NSMutableArray *contentArry;
@property (nonatomic, strong) SDUserModel *Smodel;

@end

@implementation JWHongSearchResultVC

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"--searchText--%@", self.searchText);
   
    [self setdata];
    [self setBack];
  [self.view addSubview:self.tableView];
    self.dataSource=[[LMDataSource alloc]initWithIdentifier:reuserId configureBlock:^(JWSearcell *cell, id model, NSIndexPath *indexPath) {
      self.Smodel=self.contentArry[indexPath.row];
        cell.nameLab.text=self.Smodel.name;
        cell.specialLab.text=self.Smodel.goal;
    }];
    
    
    self.tableView.delegate=self;
    self.tableView.dataSource=self.dataSource;
}
-(void)setBack{
    /*设置导航栏上面的内容*/
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"navigationbar_back_highlighted"] forState:UIControlStateHighlighted];
    // 设置尺寸
    backBtn.size = backBtn.currentBackgroundImage.size;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    /**
     解决自定义返回 手势失效
     */
self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;
}
#pragma mark - tableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JWStudentInfoCtry *vc=[[JWStudentInfoCtry alloc]init];
    self.Smodel=self.contentArry[indexPath.row];
    vc.title=[NSString stringWithFormat:@"%@的信息",self.Smodel.name];
    [JWUserManager saveSD:self.Smodel];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setdata{
    NSDictionary *dic=@{
                        @"jwid":JWid,
                        @"jwpassword":JWpassword,
                        @"searchname":self.searchText,
                        };
    [[AFNetworkTool shareManager] postJSONWithUrl:JW_HomeSearch parmas:dic successData:^(id json) {
        if([json[@"messages"] isEqualToString:@"success"]){
             NSArray *arry=json[@"studentlist"];
            for(int i=0;i<arry.count ;i++){
                self.Smodel=[SDUserModel yy_modelWithJSON:arry[i]];
                [self.contentArry addObject:self.Smodel];
            }
            [self.dataSource addDataArray:self.contentArry];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }else{
            [MBProgressHUD showError:json[@"messages"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        //隐藏组与导航栏间距
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView=[[UIView alloc]init];
        
       [_tableView registerClass:[JWSearcell class] forCellReuseIdentifier:reuserId];

    }
    return _tableView;
}
- (NSMutableArray*)contentArry{
    if(!_contentArry){
        _contentArry=[[NSMutableArray alloc]init];
    }
    return _contentArry;
}


@end
